package com.asj.house.service.impl;

import com.asj.house.entity.HouseNotice;
import com.asj.house.mapper.HouseNoticeMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.HouseNoticeService;
import com.asj.house.utils.TimeUtilCustomize;
import com.asj.house.vo.param.NoticeParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
public class HouseNoticeServiceImpl extends ServiceImpl<HouseNoticeMapper, HouseNotice> implements HouseNoticeService {

    @Override
    public RespEntity<List<HouseNotice>> noticeList() {
        QueryWrapper<HouseNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseNotice::getDeleted, 0);
        List<HouseNotice> noticeList = baseMapper.selectList(queryWrapper);
        if (Objects.isNull(noticeList) || noticeList.size() == 0) {
            return new RespEntity(RespCode.SUCCESS);
        }

        noticeList = noticeList.stream()
                .sorted(Comparator.comparing(HouseNotice::getId).reversed())
                .collect(Collectors.toList());

        return new RespEntity(RespCode.SUCCESS, noticeList);
    }

    @Override
    public RespEntity insertNotice(NoticeParam noticeParam) {
        String date = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_MD_ZH, new Date());

        HouseNotice notice = HouseNotice.builder()
                .time(date)
                .title(noticeParam.getTitle())
                .content1(noticeParam.getContent1())
                .content2(noticeParam.getContent2())
                .url(noticeParam.getUrl())
                .build();

        baseMapper.insert(notice);
        return new RespEntity(RespCode.SUCCESS);
    }

}
